import java.awt.Rectangle;
import java.awt.geom.Point2D;


public class MovingPlatform {
	
	private Point2D.Double ulc; //upper left corners
	private Rectangle bb;
	
	private int dir = 1; //Direction... 1 will go from (i) to (f), -1 goes from (f) to (i)
	private double dx, dy; //position increment
	
	private int xi,yi,xf,yf;
	private int frameCounter, threshold;
	private int orient;
	
	/**
	 * Creates a new moving platform with the
	 * upper left corner (ULC) as specified
	 * within the constructor
	 * @param xi ULC.x
	 * @param yi ULC.y
	 * @param xf ULC target x
	 * @param yf ULC target y
	 * @param orient Orientation (0 = horizontal, 1 = vertical)
	 * @param timePerWalk
	 */
	public MovingPlatform( int xi, int yi, int xf, int yf, int orient, int timePerWalk, int framerate){		
		if(  orient!=0 && orient!=1  )	throw new IllegalArgumentException();
		
		//Create point to keep track, and bounding box for collisions
		ulc = new Point2D.Double( xi, yi);
		bb = new Rectangle( xi, yi, (orient==1)?40:160 , (orient==1)?160:40);
		this.orient = orient;
		//Calculate how much to move per frame
		dx = ( (double)(xf - xi) ) / (double)timePerWalk / framerate;
		dy = ( (double)(yf - yi) ) / (double)timePerWalk / framerate;
		
		//Remember initial position so we can reset.
		this.xi = xi; this.xf = xf;
		this.yi = yi; this.yf = yf;
		
		threshold = timePerWalk * framerate;
	}
	
	
	/** This method gets called once per frame in GarbooMain, and lets the MovingPlatform
	 * know to update its position in space.
	 */
	public void update(){ 
		//Do we need to switch directions?
		if(frameCounter > threshold){ //Switchy time!
			dir = ( dir == 1)?-1:1;
			frameCounter = 0;
		}		
		
		//Add the increment to the high-precision position tracking:
		ulc.x+= dir*dx;
		ulc.y+= dir*dy;
		
		//Convert the high-precision into rounded screen coordinates:
		bb.setLocation((int)Math.round(ulc.x), (int)(Math.round(ulc.y)));
				
		//Register that this frame was run through.
		frameCounter++;
	}
	
	public void reset(){
		bb.setLocation( xi, yi);
		ulc.x = xi; ulc.y = yi;
		frameCounter = 0;
		dir=1;
	}	
	public int getDrawX(){
		return bb.x;
	}
	public int getDrawY(){
		return bb.y;
	}
	public Rectangle getRect(){
		return bb;
	}
	public int getOrient(){
		return orient;
	}
	
	
}
